package serv

import (
	"context"
	"generate_grom_mode_and_dao/service/genertategrom/api/internal/svc"
	"generate_grom_mode_and_dao/service/genertategrom/api/model"
	"generate_grom_mode_and_dao/service/genertategrom/api/util"
	"github.com/tal-tech/go-zero/core/logx"
	"strings"
)

type GenerateEnumService interface {
	Create(model.Table, *[]model.TableFields) error
}

type defaultGenerateEnumService struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGenerateEnumService(ctx context.Context, svcCtx *svc.ServiceContext) *defaultGenerateEnumService {
	return &defaultGenerateEnumService{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (s defaultGenerateEnumService) Create(table model.Table, fields *[]model.TableFields) error {
	var err error
	//生成dao文件
	var builderEnum strings.Builder
	_, err = builderEnum.WriteString("package " + s.svcCtx.Config.GenerateModel.SavePackageName + "\n\n")
	if nil != err {
		return err
	}
	//
	enumName := util.Marshal(table.Name) + "Enum"
	enumNameObject := enumName + "Object"
	_, err = builderEnum.WriteString("//declare structure" + "\n")

	_, err = builderEnum.WriteString("type " + enumName + " struct {\n")
	_, err = builderEnum.WriteString("\t// todo: add your enum here and delete this line reference this demo\n")
	_, err = builderEnum.WriteString("\t" + "// todo: `demo` tinyint(1) DEFAULT '1' COMMENT '1为成功，0为失败'\n")
	_, err = builderEnum.WriteString("\t" + "DEMO_MAP     map[int]string" + "\n")
	_, err = builderEnum.WriteString("\t" + "DEMO_SUCCESS int" + "\n")
	_, err = builderEnum.WriteString("\t" + "DEMO_ERROR int" + "\n")
	_, err = builderEnum.WriteString("}" + "\n")
	_, err = builderEnum.WriteString("//declare struct variable" + "\n")

	_, err = builderEnum.WriteString("var " + enumNameObject + " " + enumName + "\n")
	//
	_, err = builderEnum.WriteString("//Initialize enumerations and enumeration collections" + "\n")

	_, err = builderEnum.WriteString("func init() {" + "\n")

	_, err = builderEnum.WriteString("\t// todo: add your enum value and delete this line reference this demo\n")
	_, err = builderEnum.WriteString("\t" + "// todo: `demo` tinyint(1) DEFAULT '1' COMMENT '1为成功，0为失败'\n")

	_, err = builderEnum.WriteString("\t" + enumNameObject + ".DEMO_SUCCESS = 1\n")
	_, err = builderEnum.WriteString("\t" + enumNameObject + ".DEMO_ERROR = 0\n")

	_, err = builderEnum.WriteString("\t" + enumNameObject + ".DEMO_MAP = map[int]string{" + "\n")
	_, err = builderEnum.WriteString("\t" + "\t" + enumNameObject + ".DEMO_SUCCESS: \"成功\"," + "\n")
	_, err = builderEnum.WriteString("\t" + "\t" + enumNameObject + ".DEMO_ERROR: \"失败\"," + "\n")
	_, err = builderEnum.WriteString("\t" + "}" + "\n")

	_, err = builderEnum.WriteString("}\n" + "\n")

	fileNameDao := s.svcCtx.Config.GenerateModel.SavePath + table.Name + "enum.go"
	if nil != err {
		return err
	}
	util.MakeFile(s.svcCtx.Config.GenerateModel.SavePath, fileNameDao, builderEnum.String(), s.svcCtx.Config.GenerateModel.ModelCover)
	return nil
}
